package cn.thinkluck.pattern.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * 象棋工厂
 * 由于棋子都是固定的，使用享元模式，将棋子加载到内存中，只存一份
 * @Author liutao
 * @DateTime 2024/7/22 下午10:43
 */
public class ChessUnitFactory {
    private static final Map<Long, ChessUnit> CHESS_UNIT_MAP = new HashMap<>();
    static {
        CHESS_UNIT_MAP.put(1L,new ChessUnit(1L,"兵", ChessUnit.Color.BLACK));
        CHESS_UNIT_MAP.put(2L,new ChessUnit(2L,"马", ChessUnit.Color.BLACK));
        CHESS_UNIT_MAP.put(3L,new ChessUnit(3L,"炮", ChessUnit.Color.BLACK));
        CHESS_UNIT_MAP.put(4L,new ChessUnit(4L,"将", ChessUnit.Color.BLACK));
        CHESS_UNIT_MAP.put(5L,new ChessUnit(5L,"相", ChessUnit.Color.BLACK));
        CHESS_UNIT_MAP.put(6L,new ChessUnit(6L,"车", ChessUnit.Color.BLACK));
    }

    /**
     * 暴露一个工厂方法，用来获取棋子
     * @param id 棋子id
     * @return 棋子
     */
    public static ChessUnit getChessUnit(Long id) {
        return CHESS_UNIT_MAP.get(id);
    }
}
